package com.jlastu.znb.view;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;

import com.jlastu.znb.R;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private LinearLayout mTabOne;
    private LinearLayout mTabTwo;
    private LinearLayout mTabAdd;
    private LinearLayout mTabMessage;
    private LinearLayout mTabMine;
    private BuyerHomeFragment mBuyerHomeFragment;
    private MineFragment mMineFragment;
    private SchoolFragment mSchoolFragment;
    private MessageFragment mMessageFragment;
    private Fragment mContentFragment;
    private FragmentManager mFragmentManager;
    public static final String EXTRA_SHOW_SCHOOL_FRAGMENT = "SHOW_SCHOOL_FRAGMENT";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabOne = findViewById(R.id.ll_tab_one);
        mTabTwo = findViewById(R.id.ll_tab_two);
        mTabAdd = findViewById(R.id.ll_tab_add);
        mTabMessage = findViewById(R.id.ll_tab_message);
        mTabMine = findViewById(R.id.ll_tab_mine);

        mTabOne.setOnClickListener(this);
        mTabTwo.setOnClickListener(this);
        mTabAdd.setOnClickListener(this);
        mTabMessage.setOnClickListener(this);
        mTabMine.setOnClickListener(this);
        initFragment();
        boolean showSchoolFragment = getIntent().getBooleanExtra(EXTRA_SHOW_SCHOOL_FRAGMENT, false);
        if (showSchoolFragment) {
            // 如果Intent中有标志显示SchoolFragment，则显示它
            showSchoolFragment();
        } else {
            // 否则，显示默认的BuyerHomeFragment
            changeFragment(mBuyerHomeFragment);
        }
        // 根据当前显示的Fragment设置导航栏的选中状态
        setTabSelection();
    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mBuyerHomeFragment = new BuyerHomeFragment();
        mSchoolFragment = new SchoolFragment();
        mMineFragment = new MineFragment();
        mMessageFragment = new MessageFragment();
    }

    public void changeFragment(Fragment to) {
        if (mContentFragment != to) {
            FragmentTransaction transaction = mFragmentManager.beginTransaction();
            if (mContentFragment != null) {
                transaction.hide(mContentFragment);
            }
            if (to.isAdded()) {
                transaction.show(to);
            } else {
                transaction.add(R.id.fragment_content1, to);
            }
            transaction.commitAllowingStateLoss();
            mContentFragment = to;
        }
    }
    public void showSchoolFragment() {
        if (mSchoolFragment == null || !mSchoolFragment.isVisible()) {
            mSchoolFragment = new SchoolFragment();
            changeFragment(mSchoolFragment);
        }
        mTabTwo.setSelected(true);
    }
    private void setTabSelection() {
        if (mContentFragment == mBuyerHomeFragment) {
            mTabOne.setSelected(true);
        } else if (mContentFragment == mSchoolFragment) {
            mTabTwo.setSelected(true);
        }
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.ll_tab_one) {
            setTabSelected(mTabOne, mBuyerHomeFragment);
        } else if (id == R.id.ll_tab_two) {
            setTabSelected(mTabTwo, mSchoolFragment);
        } else if (id == R.id.ll_tab_add) {
            Intent intent = new Intent(MainActivity.this, FaBuActivity.class);
            startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(
                    MainActivity.this).toBundle());
        } else if (id == R.id.ll_tab_message) {
            setTabSelected(mTabMessage, mMessageFragment);
        } else if (id == R.id.ll_tab_mine) {
            setTabSelected(mTabMine, mMineFragment);
        }
    }
    private void setTabSelected(LinearLayout tab, Fragment fragment) {
        // 取消所有选项卡的选中状态
        mTabOne.setSelected(false);
        mTabTwo.setSelected(false);
        mTabMessage.setSelected(false);
        mTabMine.setSelected(false);
        // 其他选项卡的选中状态重置可以根据需要添加

        // 选中当前点击的选项卡
        tab.setSelected(true);

        // 显示对应的Fragment
        changeFragment(fragment);
    }

    // ...其他代码...
}